package thoughtworks;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {

	public static void main(String[] args) {
		Main main = new Main();
		main.generateSummary("2016-06-02 20:00~22:00 7\n");
		main.generateSummary("2016-06-03 09:00~12:00 14");
		main.generateSummary("2016-06-04 14:00~17:00 22");
		main.generateSummary("2016-06-05 19:00~22:00 3");
		main.generateSummary("2016-06-06 12:00~15:00 15");
		main.generateSummary("2016-06-07 15:00~17:00 12");
		main.generateSummary("2016-06-08 10:00~13:00 19");
		main.generateSummary("2016-06-09 16:00~18:00 16");
		main.generateSummary("2016-06-10 20:00~22:00 5");
		main.generateSummary("2016-06-11 13:00~15:00 11");
	}
	
	//快速的查询价格
	public final int[][] price = {{30,30,30, 50,50,50,50,50,50, 80,80, 60,60},
		                    {40,40,40, 50,50,50,50,50,50,50, 60,60,60,60}};
	
	//you know, while you income is less than the cost, you can increase it
	public static final int EVERYONE_PAY = 30;
	
	public String generateSummary(String inputs) {
		String[] inputArray = inputs.split("\n");
		StringBuilder builder = new StringBuilder();
		System.out.println("[Summary]");
		System.out.println();
		int totalIncome = 0, totalPayment = 0, profit = 0;
		int peopleCount, orderCount, week, startTime, endTime;
		int cost, income, curProfit;
		for (String input : inputArray) {
			peopleCount = Integer.parseInt(input.substring(23, input.length()));
			orderCount = getPlaceCount(peopleCount);
			if(orderCount == 0){
				System.out.println(input.substring(0, 22) + " +0 -0 " + 0);
				builder.append(input.substring(0, 22) + " +0 -0 " + 0 + "\n");
			}
			week = getDayOfWeek(input.substring(0, 10));
			startTime = getPlayTime(input.substring(11, 16));
			endTime = getPlayTime(input.substring(17, 22));
			cost = 0;   // record the all cost
			income = 0;
//			System.out.println("start:" + startTime + " endTime:" + endTime);
			if(startTime < 9 || endTime > 22){
				throw new IllegalArgumentException("it's too late or early, the gym is close");
			}
			for (int i = startTime; i < endTime; i++) {
				cost += price[week][i - 9];
			}
			cost *= orderCount;
			income = peopleCount * EVERYONE_PAY;
			curProfit = income - cost;
			
			totalIncome += income;
			totalPayment += cost;
			profit += curProfit;
			System.out.println(input.substring(0, 22) + 
					" +" + income + " -" + cost + (curProfit > 0 ? " +" :" ") + curProfit);
			builder.append(input.substring(0, 22) + 
					" +" + income + " -" + cost + (curProfit > 0 ? " +" :" ") + curProfit + "\n");
		}
		System.out.println();
		System.out.println("Total Income: " + totalIncome);
        System.out.println("Total Payment: " +  totalPayment);
        System.out.println("Profit: " + profit);
		return builder.toString();
	}

	//获取活动的时间
	public int getPlayTime(String input){
		//24-hour use HH
		SimpleDateFormat sDateFormat = new SimpleDateFormat("HH:mm");
		Calendar calendar = Calendar.getInstance();
		int timeIndex = 0;
		try {
			calendar.setTime(sDateFormat.parse(input));
			//24-hour use HOUR_OF_DAY
			timeIndex = calendar.get(Calendar.HOUR_OF_DAY);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timeIndex;
	}
	
	// 周一至周五的价格一样，于是若是周一到周五那就返回0,周六到周天就返回1
	public int getDayOfWeek(String input) {
		int dayOfWeek;
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			calendar.setTime(sDateFormat.parse(input));
			dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//			calendar.get(Calendar.DATE);
			if (dayOfWeek == 1 || dayOfWeek == 7) {
				return 1;
			} else {
				GregorianCalendar calendar1 = new GregorianCalendar();
				int i = calendar.get(GregorianCalendar.AM_PM) == 1 ? 0 : 1;
				return 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	// 根据人数获取场地的数目
	public int getPlaceCount(int n) {
		if (n < 4) {
			return 0;
		}
		if (n >= 4 && n <= 6) {
			return 1;
		}
		if (n > 6 && n < 16) {
			return 2;
		}
		if (n >= 16 && n <= 21) {
			return 3;
		}
		if (n >= 22 && n <= 29) {
			return 4;
		}
		if (n >= 30) {
			return n / 6;
		}
		return 0;
	}
}
